//~ Project: wslds-blog File: ParamtersInterceptor.java Created date: Feb 21, 2012
package com.wslds.blog.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * 
 * @author $Author: donilan.qq@gmail.com $
 * @version $Revision: 83 $
 */
@SuppressWarnings("serial")
public class ParamtersInterceptor implements Interceptor {

	@Override
	public void destroy() {
		// Empty
	}

	@Override
	public void init() {
		// Empty
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		String controllerAndAction = invocation.getInvocationContext().getName();
		HttpServletRequest request = ServletActionContext.getRequest();
		if(StringUtils.isNotBlank(controllerAndAction)) {
			String controller = (String)request.getAttribute("_controller");
			String action = (String)request.getAttribute("_action");
			String[] tmp = controllerAndAction.split("/");
			if(StringUtils.isBlank(controller) && tmp != null) {
				request.setAttribute("_controller", tmp[0]);
			}
			if(StringUtils.isBlank(action) && tmp != null && tmp.length > 1) {
				request.setAttribute("_action", tmp[1]);
			}
		}
		return invocation.invoke();
	}

}
